<template>
	<view>
		<Navbar :isReturn="false"></Navbar>
		
		<Modal :show="modal.show" :confirmText="modal.confirm" :cancelText="modal.cancel" :modalContent="modal.content"
			:showCancel="modal.showCancel" @confirm="modalConfirm" @cancel="modalConfirm"></Modal>
	</view>
</template>

<script>
	import Navbar from '@/components/Navbar.vue'
	import Modal from '@/components/Modal.vue'
	import env from '@/network/env.js'
	import { InitScene } from '@/utils/index.js'
	import { getConfigInfo, bindUserId } from '@/network/api.js'
	import { teaOrderSn, teaCreateOrder, teaAdressNum } from '@/network/tea.js'
	export default{
		components:{
			Navbar,
			Modal
		},
		data(){
			return {
				modal: {
					show: false,
					showCancel: true,
					confirm: '申请',
					cancel: '取消',
					content: ''
				},
			}
		},
		async onLoad(options){
			await this.$onLaunched
			if(options.scene){
				let data = InitScene(options.scene)
				this.onAddressPay(data.add,0)
				/* 绑定关系 */
				const pages = getCurrentPages()
				const currentPage = pages[pages.length - 1]
				const params = {
					userid: uni.getStorageSync("user_id"),
					inviter: InitScene(options.scene).inviterId,
					url: currentPage.$page.fullPath || ''
				}
				bindUserId(params)
			}else{
				this.onAddressPay(options.id,1)
			}
		},
		methods:{
			async onAddressPay(id,status){
				const orderRes = await teaOrderSn()
				const priceRes = await teaAdressNum({address_id: id })
				const orderParams = {
					type: 'address',
					order_sn: orderRes.data.order_sn,
					userid: uni.getStorageSync("user_id"),
					price: priceRes.data.price,
					is_address_fee: priceRes.data.is_address_fee,
					address_id: id,
					is_renew: status
				}
				const _this = this
				uni.request({
					url: `${env.baseUrl}/api/wxapp_order/createOrder`,
					method: "post",
					data: orderParams,
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'Authorization': '',
					},
					async success(res) {
						if(res.data.err == 0){
							await uni.requestPayment({
								provider: "wxpay",
								appId: res.data.data.order_str.appId,
								timeStamp: res.data.data.order_str.timeStamp,
								nonceStr: res.data.data.order_str.nonceStr,
								package: res.data.data.order_str.package,
								signType: res.data.data.order_str.signType,
								paySign: res.data.data.order_str.paySign,
								success(res) {
									uni.redirectTo({
										url: "/pages/addressDetail/addressDetail?id=" + id
									})
								},
								fail(err) {
									uni.hideLoading()
									uni.showLoading({
										title: err,
									})
									uni.redirectTo({
										url: "/pages/addressDetail/addressDetail?id=" + id
									})
								},
								complete() {
									uni.hideLoading()
								}
							})
						}else if(res.data.err == -1){
							_this.modal.content = `${res.data.msg}`
							_this.modal.confirm = '确定'
							_this.modal.show = true							
						}
					},
					fail(err){
						uni.redirectTo({
							url: "/pages/addressDetail/addressDetail?id=" + id
						})
					}
				})
			},
			modalConfirm(){
				this.modal.show = false
				uni.switchTab({
					url: "/pages/home/home"
				})
			}
		}
	}
</script>

<style>
</style>